﻿namespace Sidvall.AuthorizationManager
{
    public partial class SystemContext
    {
        #region Public Members

        public const string SystemName = "AuthorizationManager";
        public global::Sidvall.AuthorizationManager.Net.Services.V1.Model.DataPortal DataPortal { get; set; }
        public global::Sidvall.Business.IDataPortalStrategy DataPortalStrategy { get; set; }
        public global::Sidvall.AuthorizationManager.Net.Services.V1.Model.EntityCache EntityCache { get; set; }
        public global::Sidvall.AuthorizationManager.Net.Services.V1.Model.EntityFacade EntityFacade { get; set; }
        public global::Sidvall.Resources.ResourceManager ResourceManager { get; set; }
        #region LogManager

        private global::Sidvall.Logging.LogManager _LogManager;
        public global::Sidvall.Logging.LogManager LogManager
        {
            get
            {
                if (_LogManager != null)
                    return _LogManager;
                return global::Sidvall.SystemContext.Current.LogManager;
            }
            set
            {
                _LogManager = value;
            }
        }

        #endregion
        
        #region GetGenericDataPortal

        public global::Sidvall.AuthorizationManager.Model.GenericDataPortal GetGenericDataPortal(global::Sidvall.Business.IDataPortal dataPortal)
        {
            return GetGenericDataPortal(dataPortal, global::Sidvall.AuthorizationManager.Net.Services.V1.Model.ResourceFactory.Current);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public global::Sidvall.AuthorizationManager.Model.GenericDataPortal GetGenericDataPortal(global::Sidvall.Business.IDataPortal dataPortal, global::Sidvall.Data.IResourceFactory resourceFactory)
        {
            return new global::Sidvall.AuthorizationManager.Model.GenericDataPortal(dataPortal, resourceFactory);
        }

        #endregion
        
        #endregion
        #region Factory Members

        static partial void OnCreateSystemContext(SystemContext item);
        private static readonly object _Mutex = new object();
        private static volatile SystemContext _Current;
        public static SystemContext Current
        {
            get
            {
                if (_Current == null)
                {
                    lock (_Mutex)
                    {
                        if (_Current == null)
                        {
                            var item = new SystemContext()
                            {
                                DataPortal = new global::Sidvall.AuthorizationManager.Net.Services.V1.Model.DataPortal(
                                    new global::Sidvall.AuthorizationManager.Model.Local.DataPortalFactory(),
                                    new global::Sidvall.AuthorizationManager.Model.Remote.DataPortalFactory(),
                                    false
                                ),
                                EntityCache = new global::Sidvall.AuthorizationManager.Net.Services.V1.Model.EntityCache(),
                                EntityFacade = new global::Sidvall.AuthorizationManager.Net.Services.V1.Model.EntityFacade(),
                                ResourceManager = new global::Sidvall.Resources.ResourceManager(
                                     new global::Sidvall.AuthorizationManager.Resources.ResourceProvider()
                                ),
                            };
                            OnCreateSystemContext(item);
                            _Current = item;
                        }
                    }
                }
                return _Current;
            }
            set
            {
                lock (_Mutex)
                {
                    _Current = value;
                }
            }
        }

        #endregion
        #region Constructors

        public SystemContext()
        {
        }

        #endregion
    }
}